package Dao;

import DataClass.Paper;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class PaperDao {

    private static PreparedStatement getPaperStatement = null;

    public static List<Paper> getPaper() throws SQLException {
        List<Paper> list = new ArrayList<Paper>();
        ResultSet data;
        try{
            data = PaperDao.getPaperData();
        }catch (SQLException e){
            System.out.println("论文查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()){
            Integer FID = data.getInt("FID");

            String FName = data.getString("FName");
            String FContent = data.getString("FContent");
            String FAuthor = data.getString("FAuthor");
            Integer Ftime = data.getInt("Ftime");

            list.add(new Paper(FID, FName, FContent, FAuthor, Ftime));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }

    /**
     * 获取论文信息
     */
    public static ResultSet getPaperData() throws SQLException {

        if(getPaperStatement == null)
        {
            String sql = "SELECT * FROM Research_Findings";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getPaperStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet result = getPaperStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}
